package cn.edu.sdjzu.xg.controller;

import cn.edu.sdjzu.xg.domain.School;
import cn.edu.sdjzu.xg.exception.BysjException;
import cn.edu.sdjzu.xg.service.SchoolService;
import cn.edu.sdjzu.xg.util.Condition;
import cn.edu.sdjzu.xg.util.ControllerHelper;
import cn.edu.sdjzu.xg.util.JSONUtil;
import cn.edu.sdjzu.xg.util.Pagination;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;

@WebServlet("/schoolRead.ctl")
public class SchoolReadController  extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, BysjException, IOException {
        resp.setContentType("application/json;charset=utf-8");
        JSONObject message = new JSONObject();
        String id_str=req.getParameter("id");
        Integer id_int=Integer.parseInt(id_str);
        try{

            School school= SchoolService.getInstance().find(id_int);

            String school_json= JSON.toJSONString(school, SerializerFeature.DisableCircularReferenceDetect);

            resp.getWriter().println(school_json);

        }catch (BysjException e){
            message.put("message",e.getMessage());
            resp.getWriter().println(message);
            e.printStackTrace();


        } catch (SQLException e){
            message.put("message", "数据库操作异常！");
            resp.getWriter().println(message);
            e.printStackTrace();


        }catch (Exception e){
            message.put("message", "网络异常！");
            resp.getWriter().println(message);
            e.printStackTrace();

        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/json;charset=utf-8");
        JSONObject message_jsonObj = new JSONObject();
        String req_jsonStr = JSONUtil.getJSON(req);
        JSONObject req_jsonObj = JSON.parseObject(req_jsonStr);
        if (req_jsonObj==null){
            message_jsonObj.put("message","未收到请求信息");
            resp.getWriter().println(message_jsonObj);
            return;
        }

        try{
            Pagination pagination = ControllerHelper.getPagination(req_jsonObj);
            List<Condition> conditionList = ControllerHelper.getConditions(req_jsonObj);
            Collection<School> schools= SchoolService.getInstance().findAll(conditionList,pagination);

            String schools_json= JSON.toJSONString(schools, SerializerFeature.DisableCircularReferenceDetect);
            resp.getWriter().println(schools_json);


        } catch (BysjException e){
            message_jsonObj.put("message",e.getMessage());
            resp.getWriter().println(message_jsonObj);
            e.printStackTrace();


        }
        catch (SQLException e){
            message_jsonObj.put("message", "数据库操作异常！");
            resp.getWriter().println(message_jsonObj);
            e.printStackTrace();

        }catch (Exception e){
            message_jsonObj.put("message", "网络异常！");
            resp.getWriter().println(message_jsonObj);
            e.printStackTrace();

        }
    }
}
